﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
using ModelLibrary;

namespace WebC
{
    public partial class controls_deptSelect : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            sId = this.ID;
            Ajax.Utility.RegisterTypeForAjax(typeof(controls_deptSelect));
            loadInfo();
        }

        public string sId = "";

        bool IsEnable = true;

        public bool bIsEnable
        {
            get { return IsEnable; }
            set { IsEnable = value; }
        }

        int DeptId;

        public int iDeptId
        {
            get { return DeptId; }
            set { DeptId = value; }
        }

        void loadInfo()
        {
            DataSet ds = null;
            clsSys clss = null;
            try
            {
                clss = new clsSys();
                ds = clss.getDeptSelectInfo(DeptId);
            }
            catch (Exception e)
            {
                ((clsBaseForm)this.Page).errorUrl(Convert.ToString(e));
            }
            finally
            {
                if (clss != null)
                {
                    clss.Dispose();
                }
            }
            if (ds != null)
            {
                StringBuilder sbu = new StringBuilder();
                int iLayCount = ds.Tables.Count;
                string sAble = bIsEnable?"":" readOnly disabled";
                for (int i = iLayCount-1; i >= 0; i--)
                {
                    sbu.Append(string.Format(@"<div><select onchange=""get{0}dept(this)""{1}>", sId, sAble));
                    int iRowCount=ds.Tables[i].Rows.Count;
                    for(int j=0;j<iRowCount;j++)
                    {
                        sbu.Append(string.Format(@"<option value=""{0}""{2}>{1}</option>",ds.Tables[i].Rows[j]["id"]
                            ,ds.Tables[i].Rows[j]["name"],Convert.ToInt32(ds.Tables[i].Rows[j]["isselect"])==1?" selected":""));
                    }
                    sbu.Append("</select></div>");
                }
                ltrDept.Text = Convert.ToString(sbu);
            }
        }

        [Ajax.AjaxMethod]
        public string getHtmlOf(int iParDeptId, string sControlId)
        {
            StringBuilder sbu = new StringBuilder();
            clsSys clss = null;
            DataTable dt = null;
            try
            {
                clss = new clsSys();
                dt = clss.getChildrenDept(iParDeptId);
            }
            catch (Exception e)
            {
                sbu.Append(Convert.ToString(e));
            }
            finally
            {
                if (clss != null)
                {
                    clss.Dispose();
                }
            }
            if (dt != null)
            {
                int iRowCount = dt.Rows.Count;
                if (iRowCount > 1)//没下属部门的就不用选了
                {
                    sbu.Append(string.Format(@"<div><div><select onchange=""get{0}dept(this)"">", sControlId));
                    for (int i = 0; i < iRowCount; i++)
                    {
                        sbu.Append(string.Format(@"<option value=""{0}"">{1}</option>", dt.Rows[i]["id"], dt.Rows[i]["name"]));
                    }
                    sbu.Append("</select></div>");
                }
            }
            return Convert.ToString(sbu);
        }
    }
}